package com.coderyeah.cosmetology.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.coderyeah.cosmetology.entity.Order;
import com.coderyeah.cosmetology.entity.User;
import com.coderyeah.cosmetology.mapper.OrderMapper;
import com.coderyeah.cosmetology.mapper.UserMapper;
import com.coderyeah.cosmetology.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coderyeah.cosmetology.util.Result;
import com.coderyeah.cosmetology.util.ResultCode;
import com.coderyeah.cosmetology.vo.OrderParams;
import com.coderyeah.cosmetology.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lqs
 * @since 2022-05-24
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Resource
    UserMapper userMapper;

    @Override
    public Result createOrder(OrderParams orderParams) {
        String time = orderParams.getTime();
        String date = orderParams.getDate();
        String phone = orderParams.getPhone();
        String information = orderParams.getInformation();
        String username = orderParams.getUsername();
        String token = orderParams.getToken();
        //判断是否为空
        if (StrUtil.isBlank(time) || StrUtil.isBlank(date) || StrUtil.isBlank(phone) ||
                StrUtil.isBlank(information) || StrUtil.isBlank(username)) {
            return new Result(ResultCode.EMPTY.getCode(), ResultCode.EMPTY.getMsg(), null);
        }

        //通过token获取openid
        if (StrUtil.isBlank(token)) {
            return new Result(ResultCode.NO_LOGIN.getCode(), ResultCode.NO_LOGIN.getMsg(), null);
        }
        User user = queryUserByToken(token);
        //获取openid
        String openid = user.getOpenid();

        //创建订单对象
        Order order = new Order();
        order.setBusid(Long.parseLong(orderParams.getBusid()));
        order.setInformation(orderParams.getInformation());
        order.setOpenid(openid);
        order.setProid(Long.parseLong(orderParams.getProid()));
        order.setUsername(orderParams.getUsername());
        order.setUsertell(orderParams.getPhone());
        order.setOrderstate("2");
        order.setProname(orderParams.getProname());
        //预约时间
        order.setMakedate(date + " " + time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        order.setPlacedate(sdf.format(new Date()));
        this.orderMapper.createOrder(order);
        return Result.success(null);
    }

    @Override
    public Result queryOrderData(String token, String orderstate) {
        //通过token查询openid
        User user = queryUserByToken(token);
        String openid = user.getOpenid();
        List<OrderVo> orderVos = orderMapper.queryOrderData(orderstate, openid);
        return Result.success(orderVos);
    }

    private User queryUserByToken(String token) {
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getToken, token);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

}
